home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15611 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  114 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: const FAR* for a DLL
  5. Message-ID: <marnoldDpHwA9.Eq5@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <828873144.24425@uribe.demon.co.uk>
  8. Date: Sun, 7 Apr 1996 14:01:21 GMT
  9. Sender: marnold@netcom23.netcom.com
  10.  
  11. roger@uribe.demon.co.uk (Roger Uribe) writes:
  12.  
  13. >Below is an exctract from a DLL I am trying to write. The 2nd parameter
  14. >for FillRect is defined in windows.h as const RECT FAR*  Could somebody
  15. >PLEASE tell me how to set it up - I've tried everything I can think of,
  16. >plus a few suggestions from others with no success so far.
  17. >e.g.   &rect  and other attempts to define a suitable pointer.
  18.  
  19. >#include <windows.h>
  20. >//This is copied from windows.h
  21. >//int  WINAPI FillRect(HDC, const RECT FAR*, HBRUSH);  
  22.  
  23. >int FAR PASCAL _export LibMain(HANDLE hInstance,WORD wDataSeg,
  24. >    WORD wHeapSize,LPSTR lpszCmdLine)
  25. >{
  26. >    if(wHeapSize > 0)
  27. >    UnlockData(0);
  28. >    return(1);
  29. >};
  30.  
  31. >int FAR PASCAL _export myShow(int dest)
  32. >{             
  33. >int i;
  34. >long colour;
  35. >typedef struct
  36. > { int left;  int top;  int right;  int bottom;}
  37. > Srect;
  38.  
  39. >Srect rect;
  40.  
  41. Why are you using this Srect struct?  Use RECT defined in "windows.h".
  42. This is the intended type for use with FillRect() (not to mention the
  43. rest of the Windows API).
  44.  
  45. > colour = 0xFF00 ; 
  46. > i= FillRect( dest, rect, CreateSolidBrush (colour));   <--- here please
  47. > return(i);
  48. >}                                            
  49.  
  50. >c:\...\drect.c(22) : error C2115: 'argument' : incompatible types
  51. >c:\...\drect.c(22) : warning C4024: 'FillRect' : different type
  52. >                                     for formal and actual parameter 2
  53.  
  54. You are getting these errors because you aren't using the right type, 
  55. just as they suggest.  FillRect() wants a pointer to a RECT, not your
  56. own custom Srect struct.
  57.  
  58. You should be doing something like the following (taken from your above 
  59. sample, but corrected to proper Windows types and perform necessary 
  60. cleanup throughout)...
  61.  
  62.    #define STRICT
  63.    #include "windows.h"
  64.  
  65.    int FAR PASCAL _export myShow(HDC dest)
  66.    {
  67.    int i;
  68.    RECT rect;
  69.    HBRUSH brush;
  70.  
  71.    // intialize rect to something!!!
  72.  
  73.    brush = CreateSolidBrush( RGB(0, 0xFF, 0 ));
  74.    i = FillRect( dest, rect, brush );   
  75.    DeleteObject( brush );
  76.    return i;
  77.    }
  78.  
  79. You're also not intializing rect to anything, which is a problem in and
  80. of itself.  FillRect() expects the 2nd parameter to point to rectangle
  81. containing meaningful coordinates to fill.
  82.  
  83. You're also not deleting the brush you create with CreateSolidBrush(),
  84. which leaks GDI resources every time you call myShow(), which if done
  85. often enough, will eventually lead to system failure.
  86.  
  87. You're also passing CreateSolidBrush() a long.  It takes a COLORREF.
  88. Your code will also be more readable if you intialize the colour using
  89. the RBG() macro defined in "windows.h", instead of a magic number like
  90. 0xFF00.
  91.  
  92. One final note, you should also #define STRICT before including 
  93. "windows.h".  It allows your compiler to perform more type-checking on
  94. Windows types, making it more difficult to write code that uses those
  95. types incorrectly.
  96.  
  97.  
  98. Your main problem seems to be some kind of aversion to using the
  99. correct Windows types.  Is there a reason to this?
  100.  
  101. If you didn't realize you were using the wrong types, may I suggest 
  102. a good Windows programming book.  "Programming Windows" by Charles
  103. Petzold is the classic, and will expose you to these basic Windows
  104. programming details.
  105.  
  106. Regards,
  107. -------------------------------------------------------------------------
  108. Matt Arnold                       |        | ||| | |||| |  | | || ||
  109. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  110. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  111. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  112. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  113. -------------------------------------------------------------------------
  114.